//
//  WBRefreshControl.swift
//  我的微博
//
//  Created by ZhangChao on 15/9/13.
//  Copyright © 2015年 ZhangChao. All rights reserved.
//

import UIKit

// 下拉超过60开始刷新
private let RefreshMaxOffset : CGFloat = -60

class WBRefreshControl: UIRefreshControl {

    override init() {
        super.init()
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init()
        
        setupUI()
    }
    
    private func setupUI(){
        
        // 用KVO的方法监听自身frame的方法
        self.addObserver(self, forKeyPath: "frame", options: [], context: nil)
        
        // 隐藏系统自带的转轮
        tintColor = UIColor.clearColor()
        
        addSubview(refreshView)
        
        refreshView.ff_AlignInner(type: ff_AlignType.CenterCenter, referView: self, size: refreshView.bounds.size)
    }
    
    deinit{
        
        // 销毁监听
        self.removeObserver(self, forKeyPath: "frame")
    }
    
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {

        // 向上拖动不再监听方法
        if frame.origin.y > 0{
            
            return
        }
        
        // 判断是否正在刷新(uicontrol 的方法)
        if refreshing {
            
            refreshView.loadindIconRotate()
            return
        }
        
        if frame.origin.y < RefreshMaxOffset && !refreshView.rotateFlag{
            
            printLog("反过来")
            
            refreshView.rotateFlag = true
        }else if frame.origin.y >= RefreshMaxOffset && refreshView.rotateFlag{
            
            printLog("转回去")
            
            refreshView.rotateFlag = false
        }

    }
    
    // 重写父类的endRefreshing方法
    override func endRefreshing() {
        super.endRefreshing()
        
        refreshView.stopAnimation()
    }
    
    // 懒加载控件
    private lazy var refreshView = WBRefreshView.refreshView()
}

class WBRefreshView: UIView {
    
    @IBOutlet weak var pullDownIcon: UIImageView!
    
    @IBOutlet weak var tipView: UIView!
    
    @IBOutlet weak var loadingIcon: UIImageView!
    
    private var rotateFlag = false {
        didSet{
            
            rotateIcon()
        }
    }
    
    class func refreshView () -> WBRefreshView {
        
        return NSBundle.mainBundle().loadNibNamed("WBRefresh", owner: nil, options: nil).last! as! WBRefreshView
    }
    
    // 下拉的箭头动画
    private func rotateIcon (){
        
        var angel = CGFloat(M_PI)
        
        angel += rotateFlag ? -0.01 : 0.01
        
        UIView.animateWithDuration(0.5) { () -> Void in
            
            self.pullDownIcon.transform = CGAffineTransformRotate(self.pullDownIcon.transform, angel)
        }
    }
    
    // 加载视图的旋转动画
    private func loadindIconRotate(){
        // 隐藏上面的视图
        tipView.hidden = true
        
        // 这个key值可以防止动画多次被加载
        let loadingKey = "loadingKey"
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.duration = 1
        
        loadingIcon.layer.addAnimation(animation, forKey: loadingKey)
    }
    
    // 停止动画
    private func stopAnimation(){
        
        // 显示上面的视图
        tipView.hidden = false
        
        loadingIcon.layer.removeAllAnimations()
    }
}